package com.inter.law.dao;

import java.sql.Timestamp;
import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.inter.law.entity.IlTask;

/**
 * A data access object (DAO) providing persistence and search support for
 * IlTask entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 *
 * @see com.inter.law.dao.IlTask
 * @author MyEclipse Persistence Tools
 */

public class IlTaskDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory.getLogger(IlTaskDAO.class);
	// property constants
	public static final String CASE_ID = "caseId";
	public static final String LAWER_ID = "lawerId";
	public static final String IS_MANAGER = "isManager";
	public static final String CONTENT = "content";
	public static final String STATUS = "status";
	public static final String IS_WHOLE_DAY = "isWholeDay";
	public static final String ADDRESS = "address";
	public static final String TYPE = "type";
	public static final String IMPORTANT = "important";
	public static final String TASK_PROFILE = "taskProfile";

	protected void initDao() {
		// do nothing
	}

	public void save(IlTask transientInstance) {
		log.debug("saving IlTask instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(IlTask persistentInstance) {
		log.debug("deleting IlTask instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public IlTask findById(java.lang.Integer id) {
		log.debug("getting IlTask instance with id: " + id);
		try {
			IlTask instance = (IlTask) getHibernateTemplate().get(
					"com.inter.law.entity.IlTask", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(IlTask instance) {
		log.debug("finding IlTask instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding IlTask instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from IlTask as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCaseId(Object caseId) {
		return findByProperty(CASE_ID, caseId);
	}

	public List findByLawerId(Object lawerId) {
		return findByProperty(LAWER_ID, lawerId);
	}

	public List findByIsManager(Object isManager) {
		return findByProperty(IS_MANAGER, isManager);
	}

	public List findByContent(Object content) {
		return findByProperty(CONTENT, content);
	}

	public List findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	public List findByIsWholeDay(Object isWholeDay) {
		return findByProperty(IS_WHOLE_DAY, isWholeDay);
	}

	public List findByAddress(Object address) {
		return findByProperty(ADDRESS, address);
	}

	public List findByType(Object type) {
		return findByProperty(TYPE, type);
	}

	public List findByImportant(Object important) {
		return findByProperty(IMPORTANT, important);
	}

	public List findByTaskProfile(Object taskProfile) {
		return findByProperty(TASK_PROFILE, taskProfile);
	}

	public List findAll() {
		log.debug("finding all IlTask instances");
		try {
			String queryString = "from IlTask";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public IlTask merge(IlTask detachedInstance) {
		log.debug("merging IlTask instance");
		try {
			IlTask result = (IlTask) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(IlTask instance) {
		log.debug("attaching dirty IlTask instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(IlTask instance) {
		log.debug("attaching clean IlTask instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static IlTaskDAO getFromApplicationContext(ApplicationContext ctx) {
		return (IlTaskDAO) ctx.getBean("IlTaskDAO");
	}

	//通过LawerId查找日程总数
	public Integer findByLawerIdTotal(Object lawerId) {
		try {
			String queryString = "select count(*) from IlTask as model where model."
					+ LAWER_ID + "="+lawerId;
			Long numberL = (Long) getHibernateTemplate().find(queryString)
					.listIterator().next();
			long numberl=numberL;
			int counti=(int)numberl;
			Integer count=counti;
			return count;
		} catch (RuntimeException re) {
			throw re;
		}
	}
	//通过LawerId查找日程并按照startTime时间顺序降序排列
	public List findByLawerIdOrderByStartTime(Object lawerId) {
		try {
			String queryString = "from IlTask as model where model."
					+ LAWER_ID + "= ? order by startTime desc";
			return getHibernateTemplate().find(queryString, lawerId);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	//通过LawerId查找日程并按照startTime时间顺序降序排列(分页查询)
	public List findByLawerIdOrderByStartTime(Object lawerId, Integer intPage,
											  Integer pageSize) {
		Session session=null;
		try {
			String queryString = "from IlTask as model where model."
					+ LAWER_ID + "="+lawerId+"order by startTime desc";

			session=getSession();
			Query query = session.createQuery(queryString);
			query.setFirstResult((intPage-1)*pageSize);
			query.setMaxResults(pageSize);
			List list = query.list();
//			     session.close();
			return list;
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
//				if(session!=null&&session.isConnected()){
//					session.close();
//				}
			throw re;
		}
	}
}